<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品促销类
 * @package  module/base/promotion
 * @subpackage
 * @author  create by allen.qiang
 * @version 1.0
 */

quanli_require('module/base/selector/gSelectorHandler.class.php');

class promotionGoods {

    //商品促销类型：1：立减 2：特价 4：积分活动 5：赠品 6、折扣
    private $promotion_goods_type = array(1,2,3,4,5,6);

    private $dbca;

    public function __construct( &$dbca ) {
        $this->dbca = $dbca;
    }

    /**
     * 查询商品促销列表
     *
     * @param unknown_type $arg_array
     */
    public function promotionGoodsList( &$arg_array ){
        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //活动标题
        if (isset($arg_array['title']) && trim($arg_array['title']) != '') {
            $sql .= ' AND title LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['title']) . '%';
        }

        //促销类型
        if (isset($arg_array['type']) && in_array($arg_array['type'], array(1 ,2 ,3, 4 , 6))) {
            $sql .= ' AND type = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['type'];
        }

        //促销日期
        if (isset($arg_array['startDate']) && trim($arg_array['startDate']) != '') {
            $sql .= ' AND DATE(startDate) <=? AND DATE(endDate) >=?';
            $params_type .= 'ss';
            $params_array[] = $arg_array['startDate'] ;
            $params_array[] = $arg_array['startDate'] ;
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计促销行数
                $search_sql = 'SELECT COUNT(*) AS count FROM promotion_goods WHERE 1=1 ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }else{

                    $arg_array['count'] = $item_count;
                    //分页bug处理
                    if ($arg_array['count'] < $arg_array['rp']) {

                        $arg_array['page'] = 1;
                    }
                    //排序
                    if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                        $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                    //分页
                    if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                        $arg_array['page'] = 1;

                    $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);
                }
            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询商品促销信息
            $search_sql = 'SELECT * FROM promotion_goods WHERE 1=1 ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            return $temp_item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
    }

    /**
     * 删除商品促销
     *
     */
    public function deletePromotionGoods ( $arg_array ) {
        //商品促销号串检查
        if (!isset($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '缺少参数' );

        //商品促销号串截成数组
        $delete_array = explode('_', $arg_array['delete_str']);

        if (empty($delete_array))
            return array( 'code' => 400, 'message' => '请选择要删除的商品促销' );

        try {
            //循环删除商品促销
            foreach( $delete_array as &$p_l ) {
                if (!is_numeric( $p_l ))
                    throw new Exception('参数错误' , 400);

                //查询商品促销信息
                $promotion_goods = new entity('promotion_goods');
                $promotion_goods->promotionID = $p_l ;
                if ($this->dbca->fetch( $promotion_goods ))
                    $this->dbca->delete( $promotion_goods );

                //删除促销详情信息
                $this->dbca->stmt_query('DELETE FROM promotion_goods_detail WHERE promotionID=?' ,'i' , $p_l) ;
            }

            return array( 'code' => 200, 'message' => '删除成功' );
        } catch (Exception $e) {
            if ($e->getCode() > 0)
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }
    }

    /**
     *商品促销的使用和禁用
     *
     * @param unknown_type $arg_array
     * @return unknown
     */
    public function promotionGoodsStatus ( $arg_array ) {
        //参数检查
        if (!isset($arg_array['useFlg_str']) || $arg_array['useFlg_str'] == '')
            return array( 'code' => 400, 'message' => '缺少参数' );

        $str_array = explode('_', $arg_array['useFlg_str']);
        try{
            //检查商品促销是否存在或已经被操作
            $promotionGoods = new entity('promotion_goods');
            $promotionGoods->promotionID = $str_array[0] ;
            if (!$this->dbca->fetch( $promotionGoods ))
                throw new Exception(sprintf('促销号「%d」,的商品促销不存在,请审核后再操作',$str_array[0]) , 400) ;

            //检查商品促销状态是否已经被修改
            if($promotionGoods->useFlg == $str_array[1] && $str_array[1] == 1)
                throw new Exception(sprintf('促销号「%d」,的商品促销已启用',$str_array[0]) , 400) ;

            if($promotionGoods->useFlg == $str_array[1] && $str_array[1] == 0)
                throw new Exception(sprintf('促销号「%d」,的商品促销已禁用',$str_array[0]) , 400) ;

            //保存和更新商品促销
            $promotionGoods->useFlg = $str_array[1] ;
            $this->dbca->save( $promotionGoods , 'promotion' );

        }catch (Exception $e) {
            if ($e->getCode() > 0)
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }

        if($str_array[1] == 1)
            return array( 'code' => 200, 'message' => '商品促销启用成功' );

        else if ($str_array[1] == 0)
            return array( 'code' => 200, 'message' => '商品促销禁用成功' );
    }

    /**
     * 保存商品促销
     *
     * @param unknown_type $arg_array
     */
    public function savePromotionGoodsInfo( $arg_array ){

        global $G_SHOP;

        //参数检查
        if(!isset($arg_array['type'] ,$arg_array['promotionID'] ,$arg_array['title'], $arg_array['startTime'] ,$arg_array['creator'] , $arg_array['endTime'] ,$arg_array['description']))
            return array( 'code' => 400, 'message' => '缺少参数' );

        $type  	   	 = $arg_array['type'] ;
        $title 	   	 = htmlspecialchars(trim($arg_array['title'])) ;
        $startTime 	 = $arg_array['startTime'] ;
        $endTime   	 = $arg_array['endTime'] ;
        $priority  	 = $arg_array['priority'] ;
        $repel     	 = $arg_array['repel'] ;
        $useFlg    	 = $arg_array['useFlg'] ;
        $description = htmlspecialchars(trim($arg_array['description'])) ;
        $promotionID = $arg_array['promotionID'] ;

        if(!isset($arg_array['use_card'])){
            return array( 'code' => 400, 'message'=> '参数错误' );
        }

        $use_card_face_value	 = '' ;

        if(isset($arg_array['use_card_face_value']) && !empty($arg_array['use_card_face_value'])){
            $use_card_face_value = implode(', ',$arg_array['use_card_face_value']);
        }

        if($arg_array['use_card'] != 1){
            $use_card_face_value	 = '' ;
        }

        $subtractMoney = 0 ;		//立减金额
        $points		   = 0 ;		//积分倍数
        $selectorID	   = 0 ; 		//商品筛选器
        $goodsArray	   = array() ;  //商品明细

        //规则参数查检
        $error_msg = '' ;
        if(!in_array($type , $this->promotion_goods_type))
            $error_msg .= '商品促销类型参数错误</br>';

        if($title == '')
            $error_msg .= '促销活动标题不能为空</br>' ;

        if($startTime == '' || $endTime == '')
            $error_msg .= '促销开始和结束时间不能为空</br>' ;

        if($type == 1){
            if(!isset($arg_array['concessions_' . $type] , $arg_array['selectorID_' . $type] ))
                return array( 'code' => 400, 'message' => '缺少参数' );

            //立减规则参数
            $selectorID    = $arg_array['selectorID_' . $type] ;
            $subtractMoney = $arg_array['concessions_' . $type] ;
        }elseif( $type == 2 ){
            if(!isset($arg_array['concessions_' . $type] , $arg_array['selectorID_' . $type] ))
                return array( 'code' => 400, 'message' => '缺少参数' );

            //特价规则参数
            $subtractMoney = $arg_array['concessions_' . $type] ;
            $selectorID  = $arg_array['selectorID_' . $type] ;
        }elseif( $type == 3 ){
            if(!isset( $arg_array['selectorID_' . $type]))
                return array( 'code' => 400, 'message' => '缺少参数' );

            //免邮费规则参数
            $selectorID  = $arg_array['selectorID_' . $type] ;

        }elseif( $type == 4 ){
            if(!isset( $arg_array['concessions_' . $type] , $arg_array['selectorID_' . $type] ))
                return array( 'code' => 400, 'message' => '缺少参数' );

            //送积分规则参数
            $points		= $arg_array['concessions_' . $type] ;
            $selectorID = $arg_array['selectorID_' . $type] ;

        }elseif( $type == 5 ){
            if(!isset( $arg_array['selectorID_' . $type]))
                return array( 'code' => 400, 'message' => '缺少参数' );

            if(!isset($arg_array['goods']) || empty($arg_array['goods']) )
                return array( 'code' => 400, 'message' => '赠品明细不能为空' );

            //商品赠品规则参数
            $selectorID  = $arg_array['selectorID_' . $type] ;

            //商品明细
            $goodsArray = $arg_array['goods'] ;

        }elseif( $type == 6 ){

            if(!isset( $arg_array['concessions_' . $type] , $arg_array['selectorID_' . $type] ))
                return array( 'code' => 400, 'message' => '缺少参数' );

            //折扣规则参数
            $subtractMoney = $arg_array['concessions_' . $type] ;
            $selectorID = $arg_array['selectorID_' . $type] ;

        }

        try{

            if($promotionID != ""){
                //检查活动优先级是否被使用
                $issetPriority = $G_SHOP->DBCA->stmt_query('SELECT COUNT(*) count FROM promotion_goods WHERE promotionID!=? AND priority=?','ii',$promotionID,$priority) ;
                if( $issetPriority[0]->count > 0 )
                    throw new Exception('促销优先级已经被使用' , 400) ;

                //检查商品明细
                $promotion_goods = array() ;
                if($goodsArray) {
                    foreach ($goodsArray as &$g_l) {
                        $goodsInfo = new entity('goods_sale') ;
                        $goodsInfo->postID = $g_l[1] ;
                        if(!$G_SHOP->DBCA->fetch($goodsInfo))
                            throw new Exception(sprintf('SKU「%s」的商品不存在' ,$g_l[0]), 400) ;

                        //查询商品是否下架
                        if($goodsInfo->useFlg == 0 || $goodsInfo->forsale == 0)
                            throw new Exception(sprintf('SKU「%s」的商品已经下架' ,$g_l[0]), 400) ;

                        //商品促销明细
                        $goods_detail = new entity('promotion_goods_detail') ;
                        $goods_detail->promotionID = $promotionID ;
                        $goods_detail->creator 	   = $arg_array['creator']  ;
                        $goods_detail->postID 	   = $g_l[1] ;
                        $goods_detail->goodsID 	   = $goodsInfo->goodsID ;
                        $goods_detail->goodsName   = $goodsInfo->goodsName ;
                        $goods_detail->salePrice   = $goodsInfo->salePrice ;
                        $goods_detail->saleAmount  = $g_l[2]  ;
                        $goods_detail->twoPostID   = $goodsInfo->twoPostID ;
                        $goods_detail->product_code= $g_l[0]  ;
                        $goods_detail->spec		   = $goodsInfo->spec ;
                        $promotion_goods[] = $goods_detail ;
                    }
                }

                //促销付款方式
                $promotion_payment = array();
                $promotion_payment_str = '';
                foreach($arg_array['paymentID'] as &$paymentID){

                    $paymentID = explode('-',$paymentID);
                    $promotion_payment_str .= $paymentID[1].',';

                    $promotionObj = new entity('promotion_payment') ;
                    $promotionObj->creator = 'system' ;
                    $promotionObj->promotionID = $promotionID ;
                    $promotionObj->payTypeID = $paymentID[0] ;
                    $promotionObj->paymentID = $paymentID[1] ;
                    $promotionObj->type = 1 ; //商品促销类型

                    $promotion_payment[] = $promotionObj;
                }

                //保存促销信息
                $save_promotion_goods = new entity('promotion_goods') ;
                $save_promotion_goods->promotionID 	 = $promotionID ;
                if(!$G_SHOP->DBCA->fetch($save_promotion_goods))
                    throw new Exception(sprintf('促销活动号「%s」的促销不存在' ,$promotionID), 400) ;

                $save_promotion_goods->editor    	 = $arg_array['creator'] ;
                $save_promotion_goods->title   	  	 = $arg_array['title'] ;
                $save_promotion_goods->startDate  	 = $arg_array['startTime'] ;
                $save_promotion_goods->endDate	  	 = $arg_array['endTime'] ;
                $save_promotion_goods->type		  	 = $type ;

                $save_promotion_goods->selectorID 	 = $selectorID ;
                $save_promotion_goods->subtractMoney = $subtractMoney ;
                $save_promotion_goods->points		 = $points ;
                $save_promotion_goods->priority		 = $priority;
                $save_promotion_goods->remark		 = $description;
                $save_promotion_goods->repel		 = $repel;
                $save_promotion_goods->useFlg		 = $useFlg;
                $save_promotion_goods->sendStartDate		= $arg_array['sendStartTime'];
                $save_promotion_goods->sendEndDate		 = $arg_array['sendEndTime'];
                $save_promotion_goods->use_card		 = $arg_array['use_card'];
                $save_promotion_goods->use_card_face_value =  $use_card_face_value;
                $save_promotion_goods->use_coupon		 = $arg_array['use_coupon'];
                $save_promotion_goods->rankID		 = $arg_array['rankID'];
                $save_promotion_goods->payTypeID		 = $arg_array['payTypeID'];
                $save_promotion_goods->promotionChannelID		 = $arg_array['promotionChannelID'];
                $save_promotion_goods->cityID		 = $arg_array['cityID'];
                $save_promotion_goods->paymentID		 = rtrim($promotion_payment_str,',');

                $G_SHOP->DBCA->save($save_promotion_goods , 'promotion' );

                //删除老的促销详情
                $G_SHOP->DBCA->stmt_query('DELETE FROM promotion_goods_detail WHERE promotionID=?' , 'i' , $promotionID);

                //删除商品促销ID下的付款方式
                $G_SHOP->DBCA->stmt_query('DELETE FROM promotion_payment WHERE promotionID=?' , 'i' , $promotionID);

                //保存付款方式
                foreach($promotion_payment as &$promotionObj){
                    $G_SHOP->DBCA->save($promotionObj) ;
                }

                //保存商品促销明细
                if($promotion_goods) {
                    foreach ($promotion_goods as &$p_l) {
                        $G_SHOP->DBCA->save($p_l , 'promotion' ) ;
                    }
                }

                return array( 'code' => 200, 'message' => '商品促销更新成功' );
            }else{
                //生成商品促销活动号
                $promotionID = promotion_seq_no() ;

                //检查活动优先级是否被使用
                $priorityInfo = new entity('promotion_goods') ;
                $priorityInfo->priority = $priority ;
                if($G_SHOP->DBCA->fetch($priorityInfo))
                    throw new Exception('促销优先级已经被使用' , 400) ;

                //检查商品明细
                $promotion_goods = array() ;
                if($goodsArray) {
                    foreach ($goodsArray as &$g_l) {
                        $goodsInfo = new entity('goods_sale') ;
                        $goodsInfo->postID = $g_l[1] ;
                        if(!$G_SHOP->DBCA->fetch($goodsInfo))
                            throw new Exception(sprintf('SKU「%s」的商品不存在' ,$g_l[0]), 400) ;

                        //查询商品是否下架
                        if($goodsInfo->useFlg == 0 || $goodsInfo->forsale == 0)
                            throw new Exception(sprintf('SKU「%s」的商品已经下架' ,$g_l[0]), 400) ;

                        //商品促销明细
                        $goods_detail = new entity('promotion_goods_detail') ;
                        $goods_detail->promotionID = $promotionID ;
                        $goods_detail->creator 	   = $arg_array['creator']  ;
                        $goods_detail->postID 	   = $g_l[1] ;
                        $goods_detail->goodsID 	   = $goodsInfo->goodsID ;
                        $goods_detail->goodsName   = $goodsInfo->goodsName ;
                        $goods_detail->salePrice   = $goodsInfo->salePrice ;
                        $goods_detail->saleAmount  = $g_l[2]  ;
                        $goods_detail->twoPostID   = $goodsInfo->twoPostID ;
                        $goods_detail->product_code= $g_l[0]  ;
                        $goods_detail->spec		   = $goodsInfo->spec ;
                        $promotion_goods[] = $goods_detail ;
                    }
                }

                //促销付款方式
                $promotion_payment = array();
                $promotion_payment_str = '';
                foreach($arg_array['paymentID'] as &$paymentID){

                    $paymentID = explode('-',$paymentID);
                    $promotion_payment_str .= $paymentID[1].',';

                    $promotionObj = new entity('promotion_payment') ;
                    $promotionObj->creator = 'system' ;
                    $promotionObj->promotionID = $promotionID ;
                    $promotionObj->payTypeID = $paymentID[0] ;
                    $promotionObj->paymentID = $paymentID[1] ;
                    $promotionObj->type = 1 ; //商品促销类型

                    $promotion_payment[] = $promotionObj;
                }

                //保存促销信息
                $save_promotion_goods = new entity('promotion_goods') ;
                $save_promotion_goods->creator    	 = $arg_array['creator'] ;
                $save_promotion_goods->title   	  	 = $arg_array['title'] ;
                $save_promotion_goods->startDate  	 = $arg_array['startTime'] ;
                $save_promotion_goods->endDate	  	 = $arg_array['endTime'] ;
                $save_promotion_goods->type		  	 = $type ;
                $save_promotion_goods->promotionID 	 = $promotionID ;
                $save_promotion_goods->selectorID 	 = $selectorID ;
                $save_promotion_goods->subtractMoney = $subtractMoney ;
                $save_promotion_goods->points		 = $points ;
                $save_promotion_goods->priority		 = $priority;
                $save_promotion_goods->repel		 = $repel;
                $save_promotion_goods->remark		 = $description;
                $save_promotion_goods->useFlg		 = $useFlg;
                $save_promotion_goods->sendStartDate		= $arg_array['sendStartTime'];
                $save_promotion_goods->sendEndDate		 = $arg_array['sendEndTime'];
                $save_promotion_goods->use_card		 = $arg_array['use_card'];
                $save_promotion_goods->use_card_face_value		 =  $use_card_face_value;
                $save_promotion_goods->use_coupon		 = $arg_array['use_coupon'];
                $save_promotion_goods->rankID		 = $arg_array['rankID'];
                $save_promotion_goods->payTypeID		 = $arg_array['payTypeID'];
                $save_promotion_goods->promotionChannelID		 = $arg_array['promotionChannelID'];
                $save_promotion_goods->cityID		 = $arg_array['cityID'];
                $save_promotion_goods->paymentID		 = rtrim($promotion_payment_str,',');

                $G_SHOP->DBCA->save($save_promotion_goods , 'promotion' );

                //保存付款方式
                foreach($promotion_payment as &$promotionObj){
                    $G_SHOP->DBCA->save($promotionObj) ;
                }

                //保存商品促销明细
                if($promotion_goods) {
                    foreach ($promotion_goods as &$p_l) {
                        $G_SHOP->DBCA->save($p_l , 'promotion' ) ;
                    }
                }

                return array( 'code' => 200, 'message' => '商品促销保存成功' );
            }

        }catch (Exception $e) {
            if ($e->getCode() > 0)
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }

    }

    /**
     * 检查单个商品的促销活动
     * @param int $postID 商品网购号
     * @param string $start_date 配送日期
     * @param int $promotionChannelID 促销渠道ID
     * @param int $cityid 城市ID
     * @param int $payTypeID 支付方式ID
     * @param bool $use_card_array 是否使用了现金卡
     * @param bool $use_coupon 是否使用了优惠券
     * @param int $paymentID 付款方式ID
     * @param int $rankID 会员等级id
     * @return array|null
     */
    public function searchGoodsPromotion( $postID, $start_date, $promotionChannelID, $cityid, $payTypeID, $use_card_array, $use_coupon, $paymentID, $rankID ){

        //查询促销活动
        $search_sql = 'SELECT * FROM promotion_goods WHERE startDate <= ? AND endDate >= ? AND useFlg = 1 ORDER BY priority DESC ';
        $goodsPromotion = $this->dbca->stmt_query($search_sql, 'ss', date(G_DEFAULT_DATETIME_FORMAT), date(G_DEFAULT_DATETIME_FORMAT));
        if (!$goodsPromotion) {
            return null;
        }


        //促销活动数组
        $promotion_array = array();

        $repel = 0; //排斥

        //实例化商品筛选器类
        $gSelectorHandler = new gSelectorHandler( $this->dbca );

        //检查促销活动
        foreach ($goodsPromotion as &$promotion) {

            //检查商品筛选器
            if (!empty($promotion->selectorID) && !$gSelectorHandler->isValid($promotion->selectorID, $postID))
                continue;

            if (!empty($promotion_array) && ($repel == 1 || $promotion->repel == 1))
                continue;

            if ($promotion->repel == 1)
                $repel = 1;

            //检查配送日期
            if ($promotion->sendStartDate && $promotion->sendEndDate && $promotion->sendStartDate != '0000-00-00' && $promotion->sendEndDate != '0000-00-00') {
                if (($start_date < $promotion->sendStartDate || $start_date > $promotion->sendEndDate)) {
                    continue;
                }
            }

            //检查促销渠道
            if ($promotion->promotionChannelID) {
                $promotionChannelID_array = explode(',', $promotion->promotionChannelID);
                if(!in_array($promotionChannelID, $promotionChannelID_array)){
                    continue;
                }
            }

            //检查城市
            if ($promotion->cityID) {
                $cityID_array = explode(',', $promotion->cityID);
                if(!in_array($cityid, $cityID_array)){
                    continue;
                }
            }

            //检查支付方式
            if ($promotion->payTypeID) {
                $payTypeID_array = explode(',', $promotion->payTypeID);
                if(!in_array($payTypeID, $payTypeID_array)){
                    continue;
                }
            }

            //检查付款方式
            if ($promotion->paymentID && !empty($paymentID)) {
                $paymentID_array = explode(',', $promotion->paymentID);
                if(!in_array($paymentID, $paymentID_array)){
                    continue;
                }
            }

            //检查会员等级
            if ($promotion->rankID) {
                $rankID_array = explode(',', $promotion->rankID);
                if(!in_array($rankID, $rankID_array)){
                    continue;
                }
            }

            //检查代金卡
            if ($promotion->use_card == 0 && !empty($use_card_array)) {
                continue;
            }
            if ($promotion->use_card ==1 && empty($use_card_array)) {
                continue;
            }
            if ($promotion->use_card == 1 && !empty($use_card_array)) {
                //代金卡面值数组
                $use_card_face_value_array = explode(',', $promotion->use_card_face_value);
                //验证面值
                $flg = 0;
                foreach($use_card_array as $card){
                    if(in_array($card['face_value'], $use_card_face_value_array)){
                        $flg++;
                        break;
                    }
                }
                if($flg == 0){
                    continue;
                }
            }

            //检查优惠券
            if($promotion->use_coupon == 0 && $use_coupon == 1){
                continue;
            }
            if($promotion->use_coupon == 1 && $use_coupon == 0){
                continue;
            }

            $promotion_array[] = array(
                'remark'    => empty($promotion->remark) ? $promotion->title : $promotion->remark,
                'money'    =>  $promotion->subtractMoney,       //立减金额
                'type'      => $promotion->type,                  //商品促销类型
                'points'    => $promotion->points,               //商品促销增送的积分(送积分类型)
                'startTime' => $promotion->startDate,
                'endTime'   => $promotion->endDate,
                'promotionID' => $promotion->promotionID,       //促销活动ID

            );

            if ($repel == 1)
                break;
        }

        return $promotion_array;

    }

    /**
     * 得到单个商品的促销活动
     * @param int $postID 商品网购号
     * @return array|null
     */
    public function getGoodsPromotion( $postID ){

        //查询促销活动
        $search_sql = 'SELECT * FROM promotion_goods WHERE startDate <= ? AND endDate >= ? AND useFlg = 1 ORDER BY priority DESC ';

        $goodsPromotion = $this->dbca->stmt_query($search_sql, 'ss', date(G_DEFAULT_DATETIME_FORMAT), date(G_DEFAULT_DATETIME_FORMAT));

        if (!$goodsPromotion)
            return null;

        //促销活动数组
        $promotion_array = array();

        $repel = 0;

        //实例化商品筛选器类
        $gSelectorHandler = new gSelectorHandler( $this->dbca );

        //检查促销活动
        foreach ($goodsPromotion as &$promotion) {

            //检查商品筛选器
            if (!empty($promotion->selectorID) && !$gSelectorHandler->isValid($promotion->selectorID, $postID))
                continue;

            if (!empty($promotion_array) && ($repel == 1 || $promotion->repel == 1))
                continue;

            if ($promotion->repel == 1)
                $repel = 1;


            $promotion_array[] = array(
                'remark'    => empty($promotion->remark) ? $promotion->title : $promotion->remark
            );

            if ($repel == 1)
                break;

        }

        return $promotion_array;

    }

}